package newx.com.control.comboBox
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;

	public class LabelItem extends Sprite
	{
		/**
		 *	数据 
		 */
		private var _data : Object;
		
		private var _skin : String;
		
		/**
		 *	按钮状态皮肤容器 
		 */
		private var _bgContainer : Sprite;
		/**
		 *	热区容器 
		 */
		private var _hotContainer: Sprite;
		
		private var _labelContainer : Sprite;
		
		private var _currentMC : Sprite;
		
		/**按钮3中状态的皮肤**/
		private var _overMC : Sprite;
		private var _downMC : Sprite;
		private var _normalMC : Sprite;
		
		/**
		 *	label 
		 */
		private var _txtLabel : TextField;
		
		/**
		 *	宽 
		 */
		private var _height : int;
		
		/**
		 *	高 
		 */
		private var _width : int;
		
		private var _downBtnWidth : int;
		
		private var _align : String = TextFieldAutoSize.LEFT;
		
		public function LabelItem(skin : String = "Comb_label", width : int = 100, height : int = 21, downBtnWidth : int = 18)
		{
			_skin = skin;
			
			_downBtnWidth= downBtnWidth;
			_height = height;
			_width = width;
			
			drawContainer();
			
			draw();
			
			addListener();
		}
		
		private function draw():void
		{
			drawBG(_skin);
			drawLabel();
			drawHotArea();
		}
		
		override public function set width(value:Number):void
		{
			_width = value;
			
			draw();
		}
		
		override public function set height(value:Number):void
		{
			_height	= value;
			
			draw();
		}
		
		private function drawContainer():void
		{
			_bgContainer = new Sprite();
			this.addChild(_bgContainer);
			
			_labelContainer = new Sprite();
			this.addChild(_labelContainer);
			
			_hotContainer = new Sprite();
			this.addChild(_hotContainer);
		}
		
		private function drawLabel():void
		{
			_txtLabel			=	new TextField();
			_txtLabel.textColor	=	parseInt(StaticIni.getIniVar("Css_control_nxComboBox", "labelColor"));
			_txtLabel.width 	= 	_width - _downBtnWidth;
			_txtLabel.height	=	_height;
			_txtLabel.x 		=	0;
			_txtLabel.y			=	3;
			_txtLabel.multiline = 	false;
			
			_labelContainer.addChild(_txtLabel);
		}
		
		private function drawBG(value:String):void
		{
			var res:MovieClip	=	ResManager.newResClass(value) as MovieClip;
			_normalMC			=	res.getChildByName("Button_normal") as MovieClip;
			_overMC			=	res.getChildByName("Button_over") as MovieClip;
			_downMC			=	res.getChildByName("Button_down") as MovieClip;
			_normalMC.x		=	_overMC.x		=	_downMC.x		=	0;
			_normalMC.y		=	_overMC.y		=	_downMC.y		=	0;
			
			_normalMC.width	=	_overMC.width	=	_downMC.width	=	_width;
			_normalMC.height=	_overMC.height	=	_downMC.height	=	_height;
			
			currentMC = _normalMC;
		}
		
		public function set skin(skin : String) : void
		{
			drawBG(skin);
		}
		
		private function drawHotArea():void
		{
			clearAll(_hotContainer);
			
			var hot : Sprite = new Sprite();
			hot.graphics.beginFill(0xff0000,0);
			hot.graphics.drawRect(0,0,_width,_height);
			hot.graphics.endFill();
			
			_hotContainer.addChild(hot);
		}
		
		private function addListener():void
		{
			_hotContainer.addEventListener(MouseEvent.MOUSE_OVER,onOver);
			_hotContainer.addEventListener(MouseEvent.MOUSE_OUT,onOut);
			_txtLabel.addEventListener(Event.CHANGE,txtChangeHandle);
		}
		
		private function txtChangeHandle(e:Event):void
		{
			dispatchEvent(new LabelItemEvent(LabelItemEvent.TEXT_INPUT,_txtLabel.text));
		}
		
		private function onOver(e : MouseEvent):void
		{
			_hotContainer.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
			_hotContainer.addEventListener(MouseEvent.MOUSE_UP,onUp);
			
			currentMC = _overMC;
		}
		
		private function onOut(e : MouseEvent):void
		{
			_hotContainer.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);
			_hotContainer.removeEventListener(MouseEvent.MOUSE_UP,onUp);
			currentMC = _normalMC;
		}
		
		private function onDown(e : MouseEvent):void
		{
			currentMC = _downMC;
		}
		
		private function onUp(e : MouseEvent):void
		{
			currentMC = _overMC;
		}

		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
			
			if(value != null){
				_txtLabel.htmlText = _data.label;
			}else{
				_txtLabel.htmlText = "";
			}
		}
		
		public function get txtTitle():String
		{
			return _txtLabel.text;
		}
		
		public function set txtTitle(val:String):void
		{
			_txtLabel.htmlText = val;
		}
		

		/**
		 *	按钮当前状态 
		 */
		private function set currentMC(value:Sprite):void
		{
			_currentMC = value;

			clearAll(_bgContainer);
			
			_bgContainer.addChild(_currentMC);
		}
		
		private function clearAll(container : Sprite):void
		{
			while(container.numChildren > 0){
				container.removeChildAt(0);
			}
		}
		
		/**
		 *	不支持右对齐 
		 * @param value
		 * 
		 */
		public function set align(value:String):void
		{
			_align = value;
			
			var textFormt : TextFormat = new TextFormat();
			textFormt.align = _align;
			_txtLabel.setTextFormat(textFormt);
			_txtLabel.defaultTextFormat = textFormt;
		}

		public function get downBtnWidth():int
		{
			return _downBtnWidth;
		}

		public function set downBtnWidth(value:int):void
		{
			_downBtnWidth = value;
			if(_txtLabel != null){
				_txtLabel.width = _width - _downBtnWidth;
			}
		}
		
		public function set txtLabelInput(value:Boolean):void
		{
			if(value)
			{
				_txtLabel.type = TextFieldType.INPUT;
				clearAll(_hotContainer);
			}
		}
	}
}